﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsMediaSelectAllDataCommand : CmsMediaSelectAllCommand
    {
        public override IQueryable<CmsMedia> Execute()
        {
            IEnumerable<CmsMedia> result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsMedia>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsMedia>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsMedia, int>>();

                result = SqlUtility<CmsMedia, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_MediaSelectAll",
                        new
                        {
                            SiteId = CommandParameter.SiteId,
                            SiteGroupId = CommandParameter.SiteGroupId,
                            MimeType = CommandParameter.MimeType,
                            WithSubfolders = CommandParameter.WithSubfolders
                        },
                        mapper);
            }

            return result != null ?
                result.AsQueryable() :
                new List<CmsMedia>().AsQueryable();
        }
    }
}
